<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function add_interest_rating($object_id, $user_id, $rating) {
    
    if(!$object_id || !$user_id)
        return FALSE;

    $id = get_interest_rating_id($object_id, $user_id);
    
    $ratingObject = get_interest_rating($id->id);
    // check if rating the same ???
    if($ratingObject->rating == $rating) {
        return $id->id;
    }
    $ret = -1;
    $timestamp = time();

	$rating_history_id = add_interest_rating_history($object_id, $user_id, $rating, $timestamp);
	
    if($id->id) { 
        $query = "UPDATE interest_ratings SET rating = '$rating', timestamp = '$timestamp', history_id = '$rating_history_id' WHERE id = '$id->id'";
        safe_update_query($query);
        $ret = $id->id;
    } else {
        $query = "INSERT INTO interest_ratings (object_id, user_id, rating, history_id, timestamp)
                VALUES ('$object_id', '$user_id', '$rating', '$rating_history_id', '$timestamp')";
        $ret = safe_insert_query($query);
    }
    update_rating($object_id, $rating, $ratingObject->rating);    
    return $rating_history_id;
    
}

function get_interest_rating($id) {
    $query = "SELECT id, object_id, user_id, rating, timestamp FROM interest_ratings WHERE id = '$id'";
    $result = safe_select_query($query);
    return $result[0];
}

function get_interest_ratings_of($user_id) {
    $query = "SELECT id, object_id, rating, history_id, user_id, timestamp FROM interest_ratings WHERE user_id = '$user_id' ORDER BY id DESC";
    return safe_select_query($query);
}

function get_interest_ratings_on($object_id) {
    $query = "SELECT id, user_id, rating, timestamp FROM interest_ratings WHERE object_id = '$object_id'";
}

function count_interest_ratings_on($object_id) {
    $query = "SELECT count(*) as rating_count FROM interest_ratings WHERE object_id = '$object_id'";
//    echo "<br>$query<br>";
    $result = safe_select_query($query);
    return $result[0]->rating_count;
}

function count_interest_ratings_with_rating_on($object_id, $rating) {
    $query = "SELECT count(*) as rating_count FROM interest_ratings WHERE object_id = '$object_id' AND rating = '$rating'";
//    echo "<br>$query<br>";
    $result = safe_select_query($query);
    return $result[0]->rating_count;
}

function get_interest_rating_id($object_id, $user_id) {
    $query = "SELECT id FROM interest_ratings WHERE object_id = '$object_id' AND user_id = '$user_id'";
    $result = safe_select_query($query);
    return $result[0];
}

function delete_interest_ratings($object_id, $user_id) {
    $query = "DELETE FROM interest_ratings WHERE object_id = '$object_id' AND user_id = '$user_id'";
    return safe_delete_query($query);
}

function count_interest_ratings_of_user($user_id, $rating) {
    $query = "SELECT count(id) as count_ratings FROM interest_ratings WHERE user_id = '$user_id' AND rating = '$rating'";
    $result = safe_select_query($query);
    return $result[0]->count_ratings;
}

function get_friends_with_rating_of_user($user_id, $object_id, $rating, $limit, $offset) {
    $query = "SELECT id, user_id, rating, timestamp 
                FROM interest_ratings 
                WHERE object_id = '$object_id' 
                AND rating = '$rating'
                AND user_id IN (SELECT friend_id FROM opinions_friends WHERE user_id = '$user_id')
                LIMIT $offset, $limit";
//    echo "<br>$query<br>";
    $result = safe_select_query($query);
    return $result;
}

?>
